package com.xcity.game.map;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.fastjson.JSON;
import com.xcity.db.entity.Player;

public class Mines {
	protected Player owner;
	protected Map<Integer, List<Long>> mine2guards;//key:mineid value:驻守员工id数组
	
	public static int DATA_VERSION = 1;
	
	public Mines(Player owner, Map<Integer, List<Long>> map) {
		this.owner = owner;
		this.mine2guards = map != null? map : new ConcurrentHashMap<Integer, List<Long>>();
	}
	
	public void clearMineMap(){
		mine2guards.clear();
	}
	
	/**
	 * 已占领数量
	 * @return
	 */
	public int mineTakenCount(){
		return mine2guards.size();
	}
	
	/** 是否已占矿 */
	public boolean mineTaken(int mineId){
		if(mine2guards.containsKey(mineId)){
			return true;
		}
		return false;
	}
	
	/** 驻守员工的idlist */
	public List<Long> getEidList(){
		List<Long> ret = new ArrayList<Long>();
		for(List<Long> list : mine2guards.values()){
			for(int i = 0; i < list.size(); i++){
				if(list.get(i) > 0L){
					ret.add(list.get(i));
				}
			}
		}
		return ret;
	}
	
	//开始占矿
	public void takeMine(int mineId, List<Long> eids){
		mine2guards.put(mineId, eids);
	}
	
	//取消占矿
	public void unTakeMine(int mineId){
		if(mine2guards.containsKey(mineId)){
			mine2guards.remove(mineId);
		}
	}
	
	public List<Long> getEidsByMineId(int mineId){
		List<Long> ret = new ArrayList<Long>();
		if(!mine2guards.containsKey(mineId)){
			return ret;
		}
		List<Long> list = mine2guards.get(mineId);
		for(int i = 0, size = list.size(); i < size; i++){
			if(list.get(i) > 0){
				ret.add(list.get(i));
			}
		}
		return ret;
	}
	
	public Map<Integer, List<Long>> tryGetMineMap(){
		return mine2guards;
	}
	
	@Override 
	public String toString(){
		return  DATA_VERSION + ";" + JSON.toJSONString(mine2guards);
	}
}
